/** 
 * chatsort.c - sort chat room userlists
 *
 * WoodPecker is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: chatsort.c 136 2008-05-03 06:27:18Z dubkat $
 */

#include "extras.h"
#include "debug.h"

static gint
sort_chat_users_by_entry(GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer userdata)
{
    PurpleConvChatBuddyFlags f1 = 0, f2 = 0;
    char *user1 = NULL, *user2 = NULL;
    gint ret = 0;

    gtk_tree_model_get(model, a, CHAT_USERS_NAME_COLUMN, &user1, CHAT_USERS_FLAGS_COLUMN, &f1, -1);
    gtk_tree_model_get(model, b, CHAT_USERS_NAME_COLUMN, &user2, CHAT_USERS_FLAGS_COLUMN, &f2, -1);

    if(user1 == NULL || user2 == NULL)
    {
        if(!(user1 == NULL && user2 == NULL))
            ret = (user1 == NULL) ? -1 : 1;
    }
    else if(f1 != f2)
    {
        /* sort more important users first */
        ret = (f1 > f2) ? -1 : 1;
    }
    else
    {
        ret = g_utf8_collate(user1, user2);
    }

    g_free(user1);
    g_free(user2);
    return ret;
}

static gint
sort_chat_users_by_alpha(GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer userdata)
{
    char *user1 = NULL, *user2 = NULL;
    gint ret = 0;

    gtk_tree_model_get(model, a, CHAT_USERS_NAME_COLUMN, &user1, -1);
    gtk_tree_model_get(model, b, CHAT_USERS_NAME_COLUMN, &user2, -1);

    if(user1 == NULL || user2 == NULL)
    {
        if(!(user1 == NULL && user2 == NULL))
            ret = (user1 == NULL) ? -1 : 1;
    }
    else
    {
        ret = g_utf8_collate(user1, user2);
    }

    g_free(user1);
    g_free(user2);
    return ret;
}


static gint
sort_chat_users_by_pic(GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer userdata)
{
    PurpleConvChatBuddyFlags f1 = 0, f2 = 0;
    gint flag_mask = 0x000F;
    char *user1 = NULL, *user2 = NULL;
    gint ret = 0;

    gtk_tree_model_get(model, a, CHAT_USERS_NAME_COLUMN, &user1, CHAT_USERS_FLAGS_COLUMN, &f1, -1);
    gtk_tree_model_get(model, b, CHAT_USERS_NAME_COLUMN, &user2, CHAT_USERS_FLAGS_COLUMN, &f2, -1);

    f1 = f1 & flag_mask;
    f2 = f2 & flag_mask;

    if(user1 == NULL || user2 == NULL)
    {
        if(!(user1 == NULL && user2 == NULL))
            ret = (user1 == NULL) ? -1 : 1;
    }
    else if(f1 != f2)
    {
        /* sort more important users first */
        ret = (f1 > f2) ? -1 : 1;
    }
    else
    {
        ret = g_utf8_collate(user1, user2);
    }

    g_free(user1);
    g_free(user2);
    return ret;
}


static struct pecker_sort_orders
{
    const char *icon;
    void *sort_funcion;
    const char *tooltip;
} const order[] = {
	/* *INDENT-OFF* */
    { PECKER_STOCK_ENTRY, sort_chat_users_by_entry, "Current sorting by entry" },
    { PECKER_STOCK_ALPHA, sort_chat_users_by_alpha, "Current sorting by alpha" },
    { PECKER_STOCK_PIC, sort_chat_users_by_pic,     "Current sorting by pic"   }
    /* *INDENT-ON* */
};


void
change_sort_order(GtkWidget * button, void *data)
{

    static int current = 0;

    current = (current + 1) % G_N_ELEMENTS(order);
    PidginConversation *gtkconv = (PidginConversation *) data;
    PidginChatPane *gtkchat = gtkconv->u.chat;
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(gtkchat->list));

    GtkBox *buttonbox = GTK_BOX(button->parent);

    gtk_widget_destroy(button);
    button = GTK_WIDGET(pidgin_pixbuf_button_from_stock(NULL, order[current].icon, PIDGIN_BUTTON_VERTICAL));
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_sort_order), gtkconv);
    gtk_tooltips_set_tip(gtkconv->tooltips, button, order[current].tooltip, NULL);
    gtk_box_pack_end(buttonbox, button, FALSE, FALSE, 0);
    gtk_widget_show(button);
    gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model), CHAT_USERS_NAME_COLUMN, order[current].sort_funcion, NULL, NULL);



}

void
add_chat_sort_functions(PurpleConversation * c)
{
    purple_debug_misc("ADD_CHAT_SORT_FUNC", "adding chatsort to conversation %s\n", c->name);
    PidginConversation *gtkconv = PIDGIN_CONVERSATION(c);
    int current = 0;
    GtkBox *iconbox = (GtkBox *) gtkconv->u.chat->list->parent;
    GtkWidget *button = pidgin_pixbuf_button_from_stock(NULL, order[current].icon,
                                                        PIDGIN_BUTTON_VERTICAL);

    gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
    gtk_tooltips_set_tip(gtkconv->tooltips, button, order[current].tooltip, NULL);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_sort_order), gtkconv);

    gtk_box_pack_end(iconbox, button, FALSE, FALSE, 0);
    gtk_widget_show(button);


}
